//
//  AskForIntoViewController.m
//  HaiGoumerMerchant
//
//  Created by apple on 17/1/20.
//  Copyright © 2017年 com.zaoguankeji.www. All rights reserved.
//

#import "AskForIntoViewController.h"
#import "VBSMSDown.h"
#import "SHTextView.h"
#import "ChooseLocationView.h"
#import "CitiesDataTool.h"
static CGFloat const kSCrollViewHeight  = 680 ;
@interface AskForIntoViewController ()<SHTextViewDelegate, UIGestureRecognizerDelegate>

///ScrollViewHeight

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewHeight;
@property (nonatomic, assign) CGFloat addHeight; ///ScrollView增加的高度,,

@property (nonatomic,strong) ChooseLocationView *chooseLocationView;
@property (nonatomic,strong) UIView  *cover;
@property (weak, nonatomic) IBOutlet SHTextView *StoreDescribeTextView;
@property (weak, nonatomic) IBOutlet UIButton *cutDownbtn;

@property (weak, nonatomic) IBOutlet UIButton *citySelectBtn;
@property (weak, nonatomic) IBOutlet UITextField *citySelectTF;
@end

@implementation AskForIntoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setEventListViewControllerItems];
    [self setUpEextView];
    [[CitiesDataTool sharedManager] requestGetData];
    [self.view addSubview:self.cover];
    // Do any additional setup after loading the view.
}

-(void)setEventListViewControllerItems{
    UIBarButtonItem *rightBackButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"fanghui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(gotoPreferences)];
    self.navigationItem.leftBarButtonItem = rightBackButton;
}

//MARK: gotoPreferences
- (void)gotoPreferences {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)setUpEextView {
    _StoreDescribeTextView.font = [UIFont systemFontOfSize:16];
    _StoreDescribeTextView.placeholder = @"说点店铺信息...";
    /** 是否可以伸缩 */
    _StoreDescribeTextView.isCanExtend = YES;
    /** 伸缩行数 */
    _StoreDescribeTextView.extendLimitRow = 100;
    /** 伸缩方向 */
    _StoreDescribeTextView.delegate = self;
    _StoreDescribeTextView.extendDirection = ExtendDown;
    _StoreDescribeTextView.layer.borderWidth = 1;
    _StoreDescribeTextView.tintColor = [UIColor blackColor];


}
#pragma mark SHTextViewDelegate
 CGFloat currentHeight = 0;
- (void)SHTextView:(SHTextView *)SHTextView textDidChanged:(NSString *)text ViewHeight:(CGFloat)height {
    NSLog(@"%@",text);
    currentHeight =  height - currentHeight;
    self.addHeight = 55;
    if (self.addHeight >= currentHeight && height > 39) {
        currentHeight = height ;
      [self updataFrame];
   
   }
}

- (void)updataFrame {
    
    self.scrollViewHeight.constant = kSCrollViewHeight + self.addHeight;

}
- (IBAction)click:(VBSMSDown *)sender {
    //开始倒计时
    [sender countDone];
}


#pragma mark -- 城市选择

- (IBAction)citySelectBtnAction:(UIButton *)sender {
    //开始倒计时
    self.cover.hidden = !self.cover.hidden;
    self.chooseLocationView.hidden = self.cover.hidden;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_chooseLocationView.frame, point)){
        return NO;
    }
    return YES;
}
- (void)tapCover:(UITapGestureRecognizer *)tap{
    
    if (_chooseLocationView.chooseFinish) {
        _chooseLocationView.chooseFinish();
    }
}

- (ChooseLocationView *)chooseLocationView{
    
    if (!_chooseLocationView) {
        _chooseLocationView = [[ChooseLocationView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 350, [UIScreen mainScreen].bounds.size.width, 350)];
        
    }
    return _chooseLocationView;
}

- (UIView *)cover{
    
    if (!_cover) {
        _cover = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _cover.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        [_cover addSubview:self.chooseLocationView];
        __weak typeof (self) weakSelf = self;
        _chooseLocationView.chooseFinish = ^{
            [UIView animateWithDuration:0.25 animations:^{
                weakSelf.citySelectTF.text = weakSelf.chooseLocationView.address;
                weakSelf.view.transform = CGAffineTransformIdentity;
                weakSelf.cover.hidden = YES;
            }];
        };
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCover:)];
        [_cover addGestureRecognizer:tap];
        tap.delegate = self;
        _cover.hidden = YES;
    }
    return _cover;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
